﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using wenshidu;
using WindowsFormsApp1.Utils;

namespace WindowsFormsApp1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void tmrMain_Tick(object sender, EventArgs e)
        {
            // 数据绑定
            this.tsslDateTime.Text = "当前时间:" + DateTime.Now.ToString();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ResetPwd resetPwd = new ResetPwd();
            resetPwd.ShowDialog();//显示为模式对话框,即必须完成子窗口,才能操作父窗口.
        }



        private void MainFrom_Load(object sender, EventArgs e)
        {
            //打开定时器
            this.tmrMain.Start();
            this.tsslDateTime.Text = "当前时间:" + DateTime.Now.ToString();
            this.tsslUserName.Text = "欢迎您, " + GlobalData.AdminUser.Name+ GlobalData.AdminUser.RoleName+"!";
        }

        private void tsbUser_Click(object sender, EventArgs e)
        {

            //限制只打开一个子窗体
            Form test = Application.OpenForms["UserForm"];  //获取窗体对象
            if ((test == null) || (test.IsDisposed)) //如果没有打开过
            {
                //打开窗口
                UserForm userForm = new UserForm();
                // 指定打开窗体的父窗体
                userForm.MdiParent = this;
                userForm.Show();
            }
            else
            {
                test.Activate(); //如果已经打开过就让其获得焦点  
                test.WindowState = FormWindowState.Normal;//使Form恢复正常窗体大小
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            QueryForm query = new QueryForm();
            query.MdiParent = this;
            query.Show();
        }
        /// <summary>
        /// 打开监控窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            //判断选中的仓库是否已经打开过监控窗体
            //模式化对话框
            //GlobalData.WarehouseId += 1;
            GlobalData.WarehouseId = 1;//默认1号仓库
            MonitorFrom monitor = new MonitorFrom(GlobalData.WarehouseId);
            monitor.MdiParent = this;
            monitor.Show();

            //monitor.ShowDialog();//显示为模式对话框,即必须完成子窗口,才能操作父窗口.
        }

        /// <summary>
        /// 退出系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //询问是否退出系统
            var result = MessageBox.Show("是否退出系统?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //如果选择是，则关闭所有窗体并退出系统
           if(result== DialogResult.Yes)
            {
                foreach (Form form in Application.OpenForms)
                {
                    if (form != this)
                    {
                        form.Close();
                    }
                }
            }
            else
            {
                e.Cancel = true; //取消退出系统
            }
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void 修改密码MToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }

}
